package com.josework.springhelloworld.service.impl;

import com.josework.springhelloworld.dao.ArticleJDBCDao;
import com.josework.springhelloworld.model.Article;
import com.josework.springhelloworld.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Joseph Xu
 * @Date: 2021/7/25 22:08
 */
@Slf4j
@Service
public class ArticleServiceJDBCImpl implements ArticleService {

    @Resource
    private ArticleJDBCDao articleJDBCDao;

    @Autowired
    private JdbcTemplate primaryJdbcTemplate;

//    @Autowired
//    private JdbcTemplate secondaryJdbcTemplate;

//    @Override
//    public int saveArticles(Article article) {
//        int res = articleJDBCDao.save(article,primaryJdbcTemplate)>0? 1 : 0;
//
//        res += articleJDBCDao.save(article,secondaryJdbcTemplate)>0? 1 : 0;
//
//        return res;
//    }

    @Override
    public int saveArticle(Article article) {
        int res = articleJDBCDao.save(article,primaryJdbcTemplate)>0? 1 : 0;
//        res += articleJDBCDao.save(article,secondaryJdbcTemplate)>0? 1 : 0;
        return res;
    }

    @Override
    public int deleteArticle(Article article) {

        return articleJDBCDao.deleteById(article,null)>0 ? 1 : 0;
    }

    @Override
    public int updateArticle(Article article) {

        return articleJDBCDao.updateById(article,null);
    }

    @Override
    public Article getArticle(Long id) {

        return articleJDBCDao.findById(id,null);
    }

    @Override
    public List<Article> getAll() {

        return articleJDBCDao.findAll(null);
    }
}
